
//添加全局守卫
import router from "@/router";
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import useUserStore from "@/store/modules/user.ts";
NProgress.configure({ showSpinner: false })

//配置白名单
const whiteList = [
  '/login','/home',
  '/hospital/register',
  '/hospital/detail',
  '/hospital/notice',
  '/hospital/close',
  '/hospital/search',
]

//前置守卫
router.beforeEach(async (to, from, next)=>{
  NProgress.start();
  //动态设置网页标题
  document.title = '尚医通-'+to.meta.title as string
  const userStore = useUserStore()
  //判断用户是否登陆
  const token = userStore.userInfo.token
  if(token){
    next()
  }else{
    if(whiteList.includes(to.path)){
      next()
    }else{
      userStore.visiable = true
      next({
        path:'/login',
        query:{
          redirect:to.fullPath
        }
      })
    }
  }
})

//后置守卫
router.afterEach(()=>{
  NProgress.done();
})
